﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExploreAndShare.Models;
using ExploreAndShare.Models.Proxies;

namespace ExploreAndShare
{
    public partial class MasterPage : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.PreRender += Pre_Render;
        }

        protected void Pre_Render(object sender, EventArgs e)
        {
            //MembershipUser p = Membership.GetUser(Page.User.Identity.Name);
            SignOutHyperLink.NavigateUrl = "~\\AccountManager\\SignOut.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);
            SignOutHyperLink.Text = "sign out";
            MyAccountHyperLink.NavigateUrl = "~\\AccountManager\\MyAccount.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);
            HomeHyperLink.NavigateUrl = "~\\Views\\Home.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);
            ShareHyperLink.NavigateUrl = "~\\Views\\SharePage.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);
            AboutHyperLink.NavigateUrl = "~\\Views\\About.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);
            if (Session["User"] == null)
            {

                var us = Membership.GetUser(Page.User.Identity.Name);
                if (us == null)
                {
                    SignOutHyperLink.Text = "LogIn";
                    SignOutHyperLink.NavigateUrl = "~\\Account\\LogIn.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);
                    lbl_UserName.Text = string.Empty;
                    lbl_UserName.Visible = false;
                    RegisterHyperLink.Visible = true;
                    RegisterHyperLink.NavigateUrl = "~\\Account\\Register.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);
                }
                Session.Add("User", Membership.GetUser(Page.User.Identity.Name));
            }
            else
            {

                lbl_UserName.Visible = true;
                RegisterHyperLink.Visible = false;

                //UserSessionCache.LoggedInUser = new User() { UserName = "greathansen" };

                MembershipUser user = (MembershipUser) Session["User"];
                lbl_UserName.Text = user.UserName;
            }
            //Membership
            //SignOutHyperLink.NavigateUrl = this.HeadLoginView.Controls[]

            //SignOutHyperLink.NavigateUrl = "~\\Account\\SignOut.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);
        }

    }
}
